//游戏状态枚举
var GameState = cc.Enum({
    Start: 1,
    End: 2,
    Repeat: 3,
});
window.GameState = GameState;
//游戏全局对象控制脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //功夫猫对象
        Cat: cc.Node,
        //狗狗管理总对象
        DogeManager: cc.Node,
        //UI管理总结点
        UI_Total: cc.Node,
        //当前屏幕宽高与锚点[内容区域]
        Screen_W: 0,
        Screen_Y: 0,
        Screen_A: cc.p(0,0),
        //得分显示节点
        Score_Show: cc.Node,
    },

    onLoad () {
        this.init_global();
        this.node.on(cc.Node.EventType.TOUCH_START,this.listener_touch,this);  //监听触摸
        
    },

    // update (dt) {},

    //分数更新
    update_score (num) {
        this.Score_Show.getComponent(cc.Label).string = num + "";
    },

    //将下挂在游戏全局对象的部分对象赋值给全局
    init_global () {
        Game.UI = this.UI_Total;
        Game.Cat = this.Cat;
        Game.Doge = this.DogeManager;
        Game.Self = this.node;
        Game.State = GameState.End;
        Game.Screen.width = this.Screen_W;
        Game.Screen.height = this.Screen_Y;
        Game.Screen.anchor = this.Screen_A;
    },

    //监听触摸操作
    listener_touch (event) {
        var distance = 0;
        var cat = Game.Cat;  //通过全局获取猫猫对象
        var cat_script = cat.getComponent("cat_self");
        var touch_pos = cc.p(0,0);  //触摸点击位置
        if(Game.State == GameState.End) {return}  //若游戏为结束状态不允许点击
        touch_pos = event.getLocation();
        //转换一个 cc.Touch（世界坐标）到一个局部坐标，该方法基于节点坐标。
        touch_pos = Game.Self.convertTouchToNodeSpaceAR(event.touch);
        distance = Commond.Distance_Point(cat,touch_pos);
        //cat_script.move_cat(0.5,touch_pos);
        //限制猫猫的攻击距离
        if(distance > 30 && distance <= 300) {
            //若猫猫处于kick(踢腿)状态则不允许再次点击
            if(cat_script.cat_action != "kick") {
                cat_script.geting_angel(cat,touch_pos);  //确定当前斜率与偏移
                cat_script.change_action("kick",touch_pos);
                cat_script.modfily_node("kick");
            }
        }
    },

    //游戏重置方法
    repeat_game () {
        var dm_sc = this.DogeManager.getComponent("doge_manager");
        var ct_sc = this.Cat.getComponent("cat_self");
        Game.Score = 0;
        Game.Count = 0;
        dm_sc.repeat_doge();
        ct_sc.repeat_state();
        this.update_score(0);  //比分显示重置
    }
});
